package com.clouddot.baipai.ui.util.Ftp;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;

import com.clouddot.baipai.infrastructure.environment.Env;
import com.clouddot.baipai.model.beam.FTPTip;
import com.clouddot.baipai.ui.util.ToastUtil;

import java.io.File;

/**
 * Created by wxy on 2018/5/28.
 */

public class DownFileUtil {
    public static void downloadFile(final Activity activity){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Env.getFTPUtil().downloadSingleFile(Env.getAppContext().upateUrl, "/mnt/sdcard",
                           "/banpai.apk", new FTPUtil.DownLoadProgressListener() {
                                @Override
                                public void onDownLoadProgress(String currentStep, final long downProcess, File file) {
                                    if (currentStep.equals(FTPTip.FTP_DOWN_SUCCESS)) {
                                        Intent install = new Intent();
                                        install.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                        install.setAction(Intent.ACTION_VIEW);
                                        install.setDataAndType(Uri.fromFile(new File("/mnt/sdcard/banpai.apk" )),
                                                "application/vnd.android.package-archive");
                                        // 这里的是你下载好的文件路径
                                        Env.getContent().startActivity(install);
                                        Env.info("-----downUpdateFile--successful");
                                    } else if (currentStep.equals(FTPTip.FTP_DOWN_LOADING)) {

                                        Env.info("-----downUpdateFile---" + downProcess + "%");

                                        activity.runOnUiThread(new Runnable() {
                                            @Override
                                            public void run() {
                                                ToastUtil.showToast(activity, "已经下载" + downProcess + "%");
                                            }
                                        });

                                    }
                                }
                            });
                } catch (Exception e) {
                    Env.getAppContext().isCancleUpdate = true;
                    e.printStackTrace();
                }
            }
        }).start();

    }


}
